﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectText : MonoBehaviour {
    /// <summary>
    /// GameBegin与Exit一定要在最前与最后
    /// </summary>
	enum SelectType
    {
        GameBegin,
        Setting,
        Exit
    }

    SelectType type = SelectType.GameBegin;

    #region 控制左右选择

    private void Change()
    {
        GetComponent<AudioPro>().Play();
        GetComponent<Text>().text = (type.ToString().Trim('_'));
    }

    public void Left()
    {
        if (type == SelectType.GameBegin)
        {
            type = SelectType.Exit;
        }
        else
        {
            type--;
        }
        Change();
    }

    public void Right()
    {
        if (type == SelectType.Exit)
        {
            type = SelectType.GameBegin;
        }
        else
        {
            type++;
        }
        Change();
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.A)|| Input.GetKeyDown(KeyCode.LeftArrow))
        {
            Left();
        }
        else if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            Right();
        }
        if(Input.GetKeyDown(KeyCode.Return)|| Input.GetKeyDown(KeyCode.KeypadEnter))
        {
            Selected();
        }
    }

    public void Selected()
    {
        StartCoroutine(Select());
    }

    IEnumerator Select()
    {
        GetComponent<AudioPro>().Play();
        yield return new WaitForSeconds(0.3f);
        switch (type){
            case SelectType.GameBegin:
                {
                    LevelManager.Instance.ToLoading();
                    break;
                }
            case SelectType.Setting:
                {
                    GameObject.FindGameObjectWithTag(Tag.setting).
                        GetComponent<SettingPanel>().ToSetting();
                    break;
                }
            case SelectType.Exit:
                {
                    Application.Quit();
                    break;
                }

        }
    }

    private void OnEnable()
    {
        GameObject.FindGameObjectWithTag(Tag.setting).
            GetComponent<SettingPanel>().NormalPanel = transform.parent.gameObject;
    }
    

    #endregion

}
